package com.ruoyi.web.controller.university;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.university.domain.UniversityCampusVo;
import com.ruoyi.university.service.IUniversityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.university.domain.UniversityCampus;
import com.ruoyi.university.service.IUniversityCampusService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 校区Controller
 * 
 * @author atzx
 * @date 2024-04-14
 */
@RestController
@RequestMapping("/university/campus")
public class UniversityCampusController extends BaseController
{
    @Autowired
    private IUniversityCampusService universityCampusService;
    @Autowired
    private IUniversityService universityService;

    /**
     * 查询校区列表
     */
    @PreAuthorize("@ss.hasPermi('university:campus:list')")
    @GetMapping("/list")
    public TableDataInfo list(UniversityCampusVo universityCampusVo)
//    public TableDataInfo list(UniversityCampus universityCampus)
    {
        startPage();
//        List<UniversityCampus> list = universityCampusService.selectUniversityCampusList(universityCampus);
        //================================
        System.out.println("接收到的数据为："+ universityCampusVo);
//        UniversityCampusVo universityCampusVo = new UniversityCampusVo();
//        if(universityCampus != null){
//            universityCampusVo.setCampusName(universityCampus.getCampusName());
//        }
        List<UniversityCampusVo> universityCampusVos = universityCampusService.selectUniversityCampusList(universityCampusVo);
        return getDataTable(universityCampusVos);
//        return getDataTable(list);
    }

    /**
     * 导出校区列表
     */
    @PreAuthorize("@ss.hasPermi('university:campus:export')")
    @Log(title = "校区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UniversityCampus universityCampus)
    {
        UniversityCampusVo universityVo = new UniversityCampusVo();
        List<UniversityCampusVo> list = universityCampusService.selectUniversityCampusList(universityVo);
        ExcelUtil<UniversityCampusVo> util = new ExcelUtil<UniversityCampusVo>(UniversityCampusVo.class);
        util.exportExcel(response, list, "校区数据");
    }

    /**
     * 获取校区详细信息
     */
    @PreAuthorize("@ss.hasPermi('university:campus:query')")
    @GetMapping(value = "/{campusId}")
    public AjaxResult getInfo(@PathVariable("campusId") Long campusId)
    {
//        UniversityCampus universityCampus = universityCampusService.selectUniversityCampusByCampusId(campusId);
//        System.out.println("指定校区ID="+campusId+"的数据是："+universityCampus);
//        return success(universityCampus);
        //==================
        UniversityCampusVo universityCampus = universityCampusService.selectUniversityCampusByCampusId(campusId);
        System.out.println("指定校区ID="+campusId+"的数据是："+universityCampus);
        return success(universityCampus);
    }

    /**
     * 新增校区
     */
    @PreAuthorize("@ss.hasPermi('university:campus:add')")
    @Log(title = "校区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UniversityCampusVo universityCampusvo)
//    public AjaxResult add(@RequestBody UniversityCampus universityCampus)
    {
//        System.out.println("获取到的校区示例："+ universityCampus);
//        int i = universityCampusService.insertUniversityCampus(universityCampus);
//        return toAjax(i);
        //===================================
        //1.获取到学校 - 校区的 数据
        System.out.println("获取到的校区示例："+ universityCampusvo);
        //2.根据获取到的所属学校名称，获取到所属学校的ID
        Long universityId = universityService.selectUniversityIdByName(universityCampusvo.getUniversity());
        if (universityId == null) {
            return toAjax(0);
        }
        System.out.println("获取到的学校ID为：" + universityId);
        //3.根据获取到的所属学校ID，封装成为一个校区对象（universityCampus）
        UniversityCampus universityCampus = new UniversityCampus();
        universityCampus.setUniversityId(universityId);//设置所属学校ID
        universityCampus.setCampusName(universityCampusvo.getCampusName());//设置校区名称
        universityCampus.setDateAdded(new Date());//设置一个创建时间
        universityCampus.setDescription(universityCampusvo.getDescription());//设置一个关于校区的描述
        //4.执行插入操作
        int i = universityCampusService.insertUniversityCampus(universityCampus);
        return toAjax(i);
    }

    /**
     * 修改校区
     */
    @PreAuthorize("@ss.hasPermi('university:campus:edit')")
    @Log(title = "校区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UniversityCampus universityCampus)
    {
        return toAjax(universityCampusService.updateUniversityCampus(universityCampus));
    }

    /**
     * 删除校区
     */
    @PreAuthorize("@ss.hasPermi('university:campus:remove')")
    @Log(title = "校区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{campusIds}")
    public AjaxResult remove(@PathVariable Long[] campusIds)
    {
        return toAjax(universityCampusService.deleteUniversityCampusByCampusIds(campusIds));
    }
}
